//用例来自  https://code.qt.io/cgit/qt/qtbase.git/tree/examples/gui/rasterwindow?h=v6.6.1 
//主要是补充了include的路径问题，因为我不设置到Qt下的每个文件夹里面
#include <QtGui/QGuiApplication>
#include <QtWidgets/QWidget>
#include <QtGui/QWindow>
#include <QtGui/QPainter>
#include <QtGui/QBackingStore>
#include <QtGui/QResizeEvent>
#include <QtCore/QScopedPointer>

class RasterWindow:public QWindow
{
    //Q_OBJECT //说你很重要，但是这是针对大型项目的，我都All in One了 还是注释掉吧
public:
    explicit RasterWindow(QWindow *parent = nullptr)
    : QWindow(parent)
    , m_backingStore(new QBackingStore(this)){
		setGeometry(100, 100, 300, 200);
    }

    virtual void render(QPainter *painter){
		painter->drawText(QRectF(0, 0, width(), height()), Qt::AlignCenter, QStringLiteral("QWindow"));
    }

public slots:
    void renderLater(){
		requestUpdate();
    }
    void renderNow(){
		if (!isExposed())
        return;

		QRect rect(0, 0, width(), height());
		m_backingStore->beginPaint(rect);

		QPaintDevice *device = m_backingStore->paintDevice();
		QPainter painter(device);

		painter.fillRect(0, 0, width(), height(), QGradient::NightFade);
		render(&painter);
		painter.end();

		m_backingStore->endPaint();
		m_backingStore->flush(rect);
    }

protected:
    bool event(QEvent *event) override{
		if (event->type() == QEvent::UpdateRequest) {
			renderNow();
			return true;
		}
		return QWindow::event(event);
    }

    void resizeEvent(QResizeEvent  *resizeEvent) override{
		m_backingStore->resize(resizeEvent->size());
    }
    void exposeEvent(QExposeEvent *event) override{
		if (isExposed())
			renderNow();
    }

private:
    QScopedPointer<QBackingStore> m_backingStore;
};

int main(int argc, char **argv)
{
    QGuiApplication app(argc, argv);

    RasterWindow window;
    window.show();

    return app.exec();
}
